<?php
error_reporting(E_ALL ^ E_NOTICE);

use Net\Event;
use Websocket\WebsocketClient;
use Websocket\WebsocketConnection;

require_once './Websocket/WebsocketClient.php';
require_once './Websocket/WebsocketConnection.php';
require_once './Net/Event.php';

$client = new WebsocketClient();

$client
    ->onReady(function() use($client) {
        echo "连接成功\n";
    
        $client->sendMessage('hello hello hello');
        sleep(1);
        $client->sendMessage('are you ok');
        sleep(3);
        $client->sendMessage('exit');
    })
    
    ->onMessage(function(Event $e) {
        echo "收到的服务器回复: {$e->getMessage()}\n";
    })
    
    ->onConnectFail(function(Event $e) use($client) {
        echo "连接失败: {$e->getErrorCode()} {$e->getErrorMessage()}\n";
        die;
    })
    
    ->onClose(function() {
        die;
    })
;

$client->connect('127.0.0.1', 30002);

WebsocketConnection::dispatch();



